<template>
  <div>
    <div class="export_box">
      <el-button type="primary" @click="visible = true">新增</el-button>
    </div>
    <el-table :data="tableData" style="width: 100%" :height="h" border>
      <el-table-column prop="name" label="单位名称" width="230" align="center">
      </el-table-column>
      <el-table-column prop="contact" label="联系人" width="120" align="center">
      </el-table-column>
      <el-table-column prop="phone" label="联系电话" width="120" align="center">
      </el-table-column>
      <el-table-column prop="address" label="联系地址" align="center">
      </el-table-column>
      <el-table-column prop="remark" label="备注" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center" width="100">
        <template slot-scope="scope">
          <el-button type="text" @click="editData(scope.row)">编辑</el-button>
          <el-button type="text" @click="delData(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page_table">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="1"
        :page-sizes="[10, 20, 30]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pag.total"
      >
      </el-pagination>
    </div>
    <!-- 录入 -->
    <el-dialog
      title="单位信息录入"
      :visible.sync="visible"
      width="450px"
      :before-close="handleClose"
    >
      <el-form :model="form" :rules="rules" ref="ruleForm">
        <el-form-item label="单位名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="form.contact"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="联系地址" prop="address">
          <el-input v-model="form.address" type="textarea" :rows="3"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="3"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submitAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getDataByKey, openDB, changeDB, deleteDB, editDB } from "@/db/indexdb";
export default {
  data() {
    return {
      visible: false,
      form: {},
      rules: {
        name: [{ required: true, message: "请输入单位名称", trigger: "blur" }],
        contact: [{ required: true, message: "请输入联系人", trigger: "blur" }],
        phone: [{ required: true, message: "请输入联系电话", trigger: "blur" }],
      },

      tableData: [],
      pag: {
        total: 0,
        page: 1,
        size: 10,
      },
      edit: false,
      h: "",
    };
  },
  created() {
    this.getList();
    this.h = document.documentElement.offsetHeight - 220;
  },
  methods: {
    getList() {
      openDB("car", "list").then(() => {
        getDataByKey("list", "公司信息表").then((res) => {
          res?.data.sort((b, a) => {
            return Number(a.id) - Number(b.id);
          });
          let start = (this.pag.page - 1) * this.pag.size;
          let end = start + this.pag.size;
          this.tableData = res?.data.slice(start, end);
          this.pag.total = res.data.length;
        });
      });
    },
    submitAdd() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          let data = { ...this.form };
          if (this.edit) {
            editDB("list", "公司信息表", data).then((res) => {
              this.$message.success("修改成功");
              this.getList();
            });
          } else {
            data.id = new Date().getTime();
            changeDB("list", "公司信息表", data).then(() => {
              this.$message.success("添加成功");
              this.getList();
            });
          }
          this.handleClose();
        } else {
          return false;
        }
      });
    },
    handleClose() {
      this.edit = false;
      this.visible = false;
      this.form = {};
    },

    delData(id) {
      this.$confirm("确定删除该条数据吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteDB("list", "公司信息表", id)
            .then(() => {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getList();
              this.visible = false;
            })
            .catch((err) => {
              this.$message.error(err);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    editData(row) {
      this.edit = true;
      this.visible = true;
      this.form = { ...row };
    },
    handleSizeChange(val) {
      this.pag.size = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.pag1.page = val;
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped>
.search_box {
  display: flex;
  flex-wrap: wrap;
  border-bottom: 1px solid #ccc;
  .el-input,
  .el-select {
    width: 200px;
    margin-right: 20px;
    margin-bottom: 20px;
  }
  .el-date-editor {
    margin-right: 20px;
    margin-bottom: 20px;
  }
  .el-button {
    margin-bottom: 20px;
  }
}
.export_box {
  display: flex;
  justify-content: flex-end;
  margin: 10px 0;
}
.page_table {
  margin-top: 20px;
  width: 100%;
  display: flex;
  justify-content: flex-end;
}
</style>
